package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.gulimall.auth.feign.ThirdPartFeignService;
import com.atguigu.gulimall.auth.vo.UserLoginVo;
import com.atguigu.gulimall.auth.vo.UserRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author xyouzi
 * @date 2022/7/30 10:52
 */
@Controller
public class LoginController {

    @Autowired
    ThirdPartFeignService thirdPartFeignService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/login.html")
    public String loginPage(HttpSession session) {
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute == null) {
            return "login";
        } else {
            return "redirect:http://gulimall.com";
        }
    }

    @GetMapping("/reg.html")
    public String regPage() {
        return "reg";
    }

    @ResponseBody
    @GetMapping("/sms/sendcode")
    public R sendCode(@RequestParam("phone") String phone) {
        Object p = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone);
        String s = JSONObject.toJSONString(p).replaceAll("\\\"","");
        if (!StringUtils.isEmpty(s) && s.equals(phone)){
            System.out.println("失败");
            return R.error(10002,"验证码访问频率过高");
        } else {
//            System.out.println("成功");
            String code = UUID.randomUUID().toString().substring(0, 5);
//            thirdPartFeignService.sendCode(phone,code);
//            System.out.println("hello");
            redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,phone+"_"+code,1, TimeUnit.MINUTES);
            return R.ok();
        }
    }

    @PostMapping("/regist")
    public String regist( UserRegistVo vo, Model model) {
//        System.out.println(vo);
        if (vo != null) {
            // 校验验证码是否与redis中的一样
            Object codeObj = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + vo.getPhone());
            String codeJson = JSONObject.toJSONString(codeObj).replaceAll("\\\"","");
            String[] code = codeJson.split("_");

            if (code[1].equals(vo.getCode())) {
                redisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX+vo.getPhone());
                System.out.println("验证码通过，调用远程服务");
                R r = memberFeignService.regist(vo);
                if (r.getCode() == 0) {
                    return "redirect:http://auth.gulimall.com/login.html";
                }
            }
        }
        return "redirect:http://auth.gulimall.com/reg.html";
    }

    @PostMapping("/login")
    public String login(UserLoginVo vo, HttpSession session) {
        R r = memberFeignService.login(vo);
        if (r.getCode() == 0) {
            Object data = r.get("data");
            String userInfo = JSONObject.toJSONString(data);
            MemberRespVo memberRespVo = JSONObject.parseObject(userInfo, MemberRespVo.class);
            session.setAttribute(AuthServerConstant.LOGIN_USER, memberRespVo);
            return "redirect:http://gulimall.com";
        } else {
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }
}
